# Packages
pacman::p_load(tidyverse, sp, leaflet)
# Get data
data <- openxlsx::read.xlsx("data/parks.xlsx")
# Logos
logo_url <- list.files("logos", full.names = T)
# Split coordinate column
data <- data %>%
tidyr::separate(Coordinates, c('Lat', 'Long'), sep = ", ") %>%
mutate(
Lat = as.numeric(Lat), Long = as.numeric(Long),
Logo_url = logo_url
)
# Load custom functions
"src" |> list.files(full.names = T) |> walk(source)
# .gpx files
# https://medium.com/@mark.reuter/parsing-gps-gpx-files-with-r-21ea0a694da8
routes <- list.files("data/gpx", recursive = T, full.names = T)
gpx <- list()
for (i in 1:length(routes)) {
# cat(paste0(routes[i], " "))
gpx[[i]] <- xml2::read_html(routes[i]) %>% # Parse
xml2::xml_find_all("//trkpt") %>% # The lat and long data
xml2::xml_attrs() %>% bind_rows() %>%
mutate(route = i)
}
gpxdf <- bind_rows(gpx) %>%
mutate(lat = as.numeric(lat), lon = as.numeric(lon))
Tilanne: 59 % puistoista käyty (24 / 41)!
# Visited icons
visited <- data %>% filter(Visit == "Joo")
visited_icons <- icons(
iconUrl = visited$Logo_url,
iconWidth = 40, iconHeight = 30,
iconAnchorX = 0, iconAnchorY = 0
)
# Not yet
notyet <- data %>% filter(Visit != "Joo")
notyet_icons <- icons(
iconUrl = "help-circle.svg",
iconWidth = 20, iconHeight = 20,
iconAnchorX = 0, iconAnchorY = 0
)
# Simple pins
# visited_icons <- icons(
# iconUrl = "pin.png",
# iconWidth = 30, iconHeight = 30,
# iconAnchorX = 0, iconAnchorY = 0
# )
# Plot
basemap <- data %>%
leaflet() %>%
addTiles() %>%
# addMarkers(
# data = visited, ~Long, ~Lat,
# label = paste0(visited$Park_short, "\n", visited$Dates),
# icon = visited_icons
# ) %>%
# addMarkers(
# data = notyet, ~Long, ~Lat,
# label = notyet$Park_short,
# icon = notyet_icons
# )
addMarkers(
data = visited, ~Long, ~Lat,
label = paste0(visited$Park_short, "\n", visited$Dates),
icon = visited_icons
) %>%
addMarkers(
data = notyet, ~Long, ~Lat,
label = notyet$Park_short,
icon = notyet_icons
)
# Add gpx routes
for (i in 1:length(routes)) {
basemap <- basemap %>%
addPolylines(
data = gpxdf %>% filter(route == i),
lat = ~lat,
lng = ~lon,
color = "#FF4500",
opacity = .9,
weight = 3
)
}
basemap
Käydyt puistot merkitty nuppineulalla ja käymättömät kysymysmerkillä.
# Only novel parks (first visits)
data_first_visits <- data %>%
drop_na(Dates) %>%
mutate(Dates = strsplit(Dates, ", ")) %>%
unnest(Dates) %>%
tidyr::separate(Dates, c("Month", "Year"), "/") %>%
group_by(Park) %>% reframe(Year = as.numeric(min(Year))) %>%
group_by(Year) %>% reframe(n = n()) %>%
# Fill in missing rows with 0 visits
complete(Year = seq(min(Year), max(Year), by = 1), fill = list(n = 0)) %>%
mutate(cumsum = cumsum(n))
# Visits by year
p1 <- data %>%
mutate(Dates = strsplit(Dates, ", ")) %>%
unnest(Dates) %>%
tidyr::separate(Dates, c("Month", "Year"), "/") %>%
mutate(Month = as.numeric(Month), Year = as.numeric(Year)) %>%
drop_na(Year) %>%
group_by(Year) %>% mutate(visit_order = row_number(Month)-.5) %>%
ggplot(aes(x = Year)) +
geom_bar(fill = "deepskyblue4") +
# geom_col(data = data_first_visits, aes(x = Year, y = n, fill = "Ensimmäinen visiitti")) +
ggimage::geom_image(
aes(y = visit_order, image = Logo_url),
size = 0.12
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_continuous(breaks = seq(0, 3000, by = 1)) +
scale_y_continuous(breaks = seq(0, 100, by = 1)) +
# scale_fill_manual(name = "Puiston tyyppi", values = rev(c("steelblue4", "#9ACD32"))) +
ylab("Käydyt puistot") + xlab("Vuosi") + ggtitle("Vieraillut puistot vuosittain")
# Total National park trips
p2 <- data %>%
mutate(Dates = strsplit(Dates, ", ")) %>%
unnest(Dates) %>%
tidyr::separate(Dates, c("Month", "Year"), "/") %>%
drop_na(Year) %>%
group_by(Year) %>%
reframe(n = sum(Visit == "Joo")) %>%
mutate(cumsum = cumsum(n)) %>%
ggplot(aes(x = as.numeric(Year), y = cumsum)) +
geom_point(aes(color = "Kaikki"), size = 2) +
geom_line(aes(group = 1, color = "Kaikki"), linewidth = 1, alpha = .5) +
geom_point(aes(color = "Uudet"), data = data_first_visits, size = 2) +
geom_line(data = data_first_visits, aes(group = 1, color = "Uudet"), linewidth = 1, alpha = .5) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_continuous(breaks = seq(0, 3000, by = 1)) +
scale_y_continuous(breaks = seq(0, 100, by = 5)) +
scale_color_manual(name = "Puiston tyyppi", values = c("deepskyblue4", "olivedrab3")) +
ylab("Käydyt puistot") + xlab("Vuosi") + ggtitle("Kumulatiivinen summa")
patchwork::wrap_plots(p1, p2, ncol = 1, heights = c(1.5, 1))
Kartta on tehty käyttäen R-paketteja sp ja
leaflet.
Koordinaatit osoitteesta: kartta.com/muut-kartat/suomen-kansallispuistot/.